package com.jike.noobmoney.wxapi;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.alipay.sdk.app.PayTask;
import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.WXEntity;
import com.jike.noobmoney.mvp.presenter.PayPresenter;
import com.jike.noobmoney.mvp.view.activity.BaseActivity;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.Logger;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by kekex on 2019/3/19.
 * 测试类
 */

public class WXPayTestActivity extends BaseActivity implements IView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    private IWXAPI iwxapi;
    private PayPresenter payPresenter;

    @Override
    protected int layoutResID() {
        return R.layout.activity_chongzhi_test;
    }

    @Override
    protected void initData() {
        tvTitle.setText("支付");
        //注册微信支付
        iwxapi = WXAPIFactory.createWXAPI(this, ConstantValue.Wechat_Appid, true);
        iwxapi.registerApp(ConstantValue.Wechat_Appid);
        payPresenter = new PayPresenter(this);

    }

    @OnClick({R.id.iv_back, R.id.tv_chongzhi,R.id.tv_zhifubao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_chongzhi:
                chongzhi();
                break;
            case R.id.tv_zhifubao:
                zhifubao();
                break;
        }
    }

    /**
     * 支付宝
     */
    private void zhifubao() {
        showProgress();
        payPresenter.zfbPay("71", "0.1", ConstantValue.RequestKey.zhifubao);
    }

    /**
     * 微信
     */
    private void chongzhi() {
        showProgress();
        payPresenter.wxPay("71", "0.1", ConstantValue.RequestKey.weixin);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgress();
        if (ConstantValue.RequestKey.weixin.equals(actionType)) {
            WXEntity wxEntity = (WXEntity) data;
            PayReq request = new PayReq();
            request.appId = wxEntity.getAppid();
            request.partnerId = wxEntity.getPartnerid();
            request.prepayId = wxEntity.getPrepayid();
            request.packageValue = wxEntity.getPackageX();
            request.nonceStr = wxEntity.getNoncestr();
            request.timeStamp = wxEntity.getTimestamp();
            request.sign = wxEntity.getPaySign();
            iwxapi.sendReq(request);
        } else if (ConstantValue.RequestKey.zhifubao.equals(actionType)) {
            String orderInfo = (String) data;
            Logger.e("xuke", "info = " + orderInfo);

            Runnable payRunnable = () -> {
                PayTask alipay = new PayTask(WXPayTestActivity.this);
                Map<String, String> result = alipay.payV2(orderInfo, true);

                Message msg1 = new Message();
                msg1.what = 100;
                msg1.obj = result;
                mHandler.sendMessage(msg1);
            };
            // 必须异步调用
            Thread payThread = new Thread(payRunnable);
            payThread.start();

        }
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100: {
                    Map<String, String> result = (Map<String, String>) msg.obj;
                    Logger.e("xuke", "result=" + result);
                    break;
                }
                default:
                    break;
            }
        }
    };

    @Override
    public void onFailed(String code, String msg) {
        dismissProgress();
        Logger.e("xuke", "msg=" + msg);
    }
}
